/**
 * 区域导航、调试开关
 */

$(document).bind("keydown.keydown_9001", function(e) {
	if (e.shiftKey && e.keyCode == 20) {
		$("*").focus(function() {
			$(this).css({
				"outline" : "2px dashed red"
			});
		});
		$("*").blur(function() {
			$(this).css({
				"outline" : "0px black"
			});
		});
	}
});

_EsdDynamicTransformationScript.navBlock.debug = true; // 是否是调试模式，调试模式有红边线，非调试模式关闭红边框
_EsdDynamicTransformationScript.navBlock.selector.activate = ".navBlock"; // 激活的采用class因为会激活多个
// 快捷键定义区
//_EsdDynamicTransformationScript.navBlock.key = {};

//_EsdDynamicTransformationScript.navBlock.key.top = "alt+1"; // (top)
//_EsdDynamicTransformationScript.navBlock.key.nav = "alt+2"; // (nav)
//_EsdDynamicTransformationScript.navBlock.key.main = "alt+3"; // (main)
//_EsdDynamicTransformationScript.navBlock.key.content = "alt+4"; // (content)
//_EsdDynamicTransformationScript.navBlock.key.left = "alt+5"; // (left)
//_EsdDynamicTransformationScript.navBlock.key.right = "alt+6"; // (right)
//_EsdDynamicTransformationScript.navBlock.key.footer = "alt+7"; // (footer)
//_EsdDynamicTransformationScript.navBlock.key.down = "alt+8"; // (down)
//_EsdDynamicTransformationScript.navBlock.key.up = "alt+9"; // (up);

// 以下变量主要用于区块跳转
_EsdDynamicTransformationScript.navBlock.sIndex = -1; // 当前栏目下标
_EsdDynamicTransformationScript.navBlock.maxIndex = -1; // 保存目前所有class="navBlock"的区域块总数量，栏目总量下标。
_EsdDynamicTransformationScript.navBlock.minIndex = 0; // 栏目数量起始下标

// 主要代码区域

// 导航跳转
_EsdDynamicTransformationScript.navBlock.keyShowSelect = function(i) {
	$(_EsdDynamicTransformationScript.navBlock.selector.activate).each(function(index, el) {
		if (i == index) {
			_EsdDynamicTransformationScript.navBlock.debugView(el);
			$(el).find("a").each(function(i, e) {
				if (i == 0) {
					$(e).focus();
				}
			});

		}
	});
}

// 获取所有class=navBlock的区域的总数量，初始化轮训所有需要跳转的块
$(_EsdDynamicTransformationScript.navBlock.selector.activate).each(function(index, ehx) {
	_EsdDynamicTransformationScript.navBlock.maxIndex++;
});



// 快捷键功能区域
// 跳转到头部alt+1(49)
$(document).keydown(function(e) {
	if (e.altKey && e.keyCode == 49) {
		_EsdDynamicTransformationScript.navBlock.keySelectScroll(_EsdDynamicTransformationScript.navBlock.selector.top);
	}

});
// 跳转到导航alt+2(50)
$(document).keydown(function(e) {
	if (e.altKey && e.keyCode == 50) {
		_EsdDynamicTransformationScript.navBlock.keySelectScroll(_EsdDynamicTransformationScript.navBlock.selector.nav);
	}

});
// 跳转到主要区域alt+3(51)
$(document).keydown(function(e) {
	if (e.altKey && e.keyCode == 51) {
		_EsdDynamicTransformationScript.navBlock.keySelectScroll(_EsdDynamicTransformationScript.navBlock.selector.main);
	}
});

// 跳转到正文区域alt+4(52)
$(document).keydown(function(e) {
	if (e.altKey && e.keyCode == 52) {
		_EsdDynamicTransformationScript.navBlock.keySelectScroll(_EsdDynamicTransformationScript.navBlock.selector.content);
	}
});

// 跳转到左区域alt+5(53)
$(document).keydown(function(e) {
	if (e.altKey && e.keyCode == 53) {
		_EsdDynamicTransformationScript.navBlock.keySelectScroll(_EsdDynamicTransformationScript.navBlock.selector.left);
	}
});

// 跳转到右区域alt+6(54)
$(document).keydown(function(e) {
	if (e.altKey && e.keyCode == 54) {
		_EsdDynamicTransformationScript.navBlock.keySelectScroll(_EsdDynamicTransformationScript.navBlock.selector.right);
	}
});
// 跳转到底部alt+7(55)
$(document).keydown(function(e) {
	if (e.altKey && e.keyCode == 55) {
		_EsdDynamicTransformationScript.navBlock.keySelectScroll(_EsdDynamicTransformationScript.navBlock.selector.footer);
	}
});
//前进alt+8(56)
$(document).keydown(function(e) {
	if (e.altKey && e.keyCode == 56) {
		if (_EsdDynamicTransformationScript.navBlock.sIndex < _EsdDynamicTransformationScript.navBlock.maxIndex) {
			_EsdDynamicTransformationScript.navBlock.sIndex++;
			_EsdDynamicTransformationScript.navBlock.keyShowSelect(_EsdDynamicTransformationScript.navBlock.sIndex);
		}
	}
});

// 后退alt+9(57)
$(document).keydown(function(e) {
	if (e.altKey && e.keyCode == 57) {
		if (_EsdDynamicTransformationScript.navBlock.sIndex > _EsdDynamicTransformationScript.navBlock.minIndex) {
			_EsdDynamicTransformationScript.navBlock.sIndex--;
			_EsdDynamicTransformationScript.navBlock.keyShowSelect(_EsdDynamicTransformationScript.navBlock.sIndex);
		}
	}
});
// 特定区域跳转
_EsdDynamicTransformationScript.navBlock.keySelectScroll = function(id) {
	if ($(id).length > 0) {
		_EsdDynamicTransformationScript.navBlock.appendIns(id,true);//插入ins
		_EsdDynamicTransformationScript.navBlock.debugView(id);
		$(id).find("a").each(function(index) {
			if (index == 0) {
				$(this).focus();
			}
		});
		_EsdDynamicTransformationScript.navBlock.appendIns(id,false);//清楚ins
	} else {
	}

}
//为了解决漂浮高等于0的情况下区域显示是一条线
_EsdDynamicTransformationScript.navBlock.appendIns=function(id,b){
	if(b==true){
		$(id).append("<ins id='esdNavBlockIns' style='clear: both;display: block;'></ins>")
	}else{
		$("#esdNavBlockIns").remove();
	}
}

if (_EsdDynamicTransformationScript.navBlock.debug == true) {
	// 如果开启调试模式，添加调试绘制矩形边线
	$("body").append("<div id='show_top' class='lineshow'></div>");
	$("body").append("<div id='show_bottom' class='lineshow'></div>");
	$("body").append("<div id='show_left' class='lineshow'></div>");
	$("body").append("<div id='show_right' class='lineshow'></div>");
	$(".lineshow").css({
		"display" : "none"
	});
}
// 开启调试模式后会显示红边
_EsdDynamicTransformationScript.navBlock.debugView = function(e) {
	if (_EsdDynamicTransformationScript.navBlock.debug == true) {
		var x = $(e).offset();
		//var w = parseInt($(e).css("width"));
		var w = $(e).outerWidth();//返回元素的宽度（包含 padding 和 border）
		//var h = parseInt($(e).css("height"));
		var h = $(e).outerHeight();//返回元素的高度（包含 padding 和 border）
		$("#show_top").css({
			"z-index" : "9999",
			"border" : "red 2px solid",
			"position" : "absolute",
			"top" : x.top,
			"left" : x.left,
			"width" : w,
			"height" : "0px"
		});
		$("#show_bottom").css({
			"z-index" : "9999",
			"border" : "red 2px solid",
			"position" : "absolute",
			"top" : x.top + h,
			"left" : x.left,
			"width" : w,
			"height" : "0px"
		});
		$("#show_left").css({
			"z-index" : "9999",
			"border" : "red 2px solid",
			"position" : "absolute",
			"top" : x.top,
			"left" : x.left,
			"width" : "0px",
			"height" : h
		});
		$("#show_right").css({
			"z-index" : "9999",
			"border" : "red 2px solid",
			"position" : "absolute",
			"top" : x.top,
			"left" : x.left + w,
			"width" : "0px",
			"height" : h
		});
		$("html,body").animate({
			scrollTop : $("#show_top").offset().top
		}, 200);
		$(".lineshow").css({
			"display" : "block"
		});
	}
}